/*
	This class use to load all the resource file. 
	All the resource must list in "resmap.xml" file(script, textures, sounds etc).
	If the format about the file "resmap.xml" is error by this engine, 
	the error message will raise and the program will soon quit.
*/
#include "Resource.h"
namespace SLG
{

	Resource::Resource(void)
		:m_pscrRoot(0),
		m_prmRoot(0),
		m_curNode(NULL)
	{
	}

	Resource::~Resource(void)
	{
	}

	bool Resource::Init()
	{
		if(!LoadRes())
		{
			//SendError();
			return false;
		}
		/*if(m_scriptfile != NULL)
		{
			LoadScript(m_scriptfile);
		}*/
		/*if(m_ptexNode!=NULL)
		{
			if(!LoadTexture(m_ptexNode))
			{
				g_err.SendErrorMessage(ERR_FUNC,"Resource::LoadTexture");
			}
		}*/
		return true;
	}

	bool Resource::NextScene()
	{
		assert(m_curNode);
		m_curNode=m_curNode->NextSiblingElement();
		if(m_curNode==NULL)
			return false;
		return true;
	}

	GAME_STATE Resource::LoadSceneState()
	{
		const char* state=m_curNode->Attribute("state");
		if(state==NULL)
		{
			::MessageBox(NULL,"LoadSceneState Failed!","Error",MB_OK);
			return END;
		}
		if(strcmp("story",state)==0)
		{
			return STORY;
		}
		else if(strcmp("fight",state)==0)
		{
			return FIGHT;
		}
		else
		{
			return END;
		}
	}

	bool Resource::LoadModel(FightManager* fm)
	{
		
		TiXmlElement* model=m_pscrRoot.FirstChild("MODEL").FirstChildElement().Element();
		if(model==NULL)
			return false;
		do
		{
			char fname[256]="./Res/Model/";
			strcat_s(fname,256,model->Attribute("ext"));
			const char* name=model->Attribute("name");
			assert(fname);
			assert(name);
			int32 id=0;
			model->Attribute("id",&id);
			if(fname&&id)
			{
				if(!fm->AddModel(id,name,fname))
					return false;
			}
			else
			{
				return false;
			}
		}
		while(model=model->NextSiblingElement());
		TiXmlElement* pos=m_pscrRoot.FirstChild("POS").FirstChildElement().Element();
		if(pos==NULL)
			return false;
		do
		{
			int32 id=0,mid=0,face;
			const char* name;
			int32 x=0,y=0,z=0;
			pos->Attribute("id",&id);
			pos->Attribute("mid",&mid);
			name=pos->Attribute("name");
			pos->Attribute("x",&x);
			pos->Attribute("y",&y);
			pos->Attribute("face",&face);
			fm->AddFigure(id,mid,name,x,y,face);
		}
		while(pos=pos->NextSiblingElement());
		return true;
	}

	bool Resource::LoadMap(World* w)
	{
		TiXmlElement* map=m_pscrRoot.FirstChildElement("MAP").Element();
		if(map==NULL)
			return false;;
		TiXmlElement* world=map->FirstChildElement("WORLD");
		if(world==NULL)
			return false;
		int32 x=0,y=0;
		world->Attribute("maxW",&x);
		world->Attribute("maxH",&y);
		world=map->FirstChildElement("TEX");
		if(world==NULL)
			return false;
		char fname[256]="./Res/World/";
		strcat_s(fname,256,world->Attribute("ext"));
		w->LoadGround(x,y,fname);
		return true;
	}

	bool Resource::LoadFightRes()
	{
		char filename[256]="./Res/XML/";
		const char* state = m_curNode->Attribute("state");
		if(state==NULL)
			return false;
		if(strcmp(state,"fight")!=0)
			return false;
		strcat_s(filename,256,m_curNode->Attribute("ext"));
		if(filename==NULL)
			return false;
		if(!m_scrdoc.LoadFile(filename))
			return false;
		m_pscrRoot=TiXmlHandle(&m_scrdoc);
		m_pscrRoot=m_pscrRoot.FirstChildElement();
		
		return true;
	}

	bool Resource::LoadRes()
	{
		if (!m_rmdoc.LoadFile(RES_MAP))
		{
			//g_err.SendErrorMessage(ERR_FILE,"resmap.xml");
			return false;
		}
		m_prmRoot=TiXmlHandle(&m_rmdoc);
		m_prmRoot=m_prmRoot.FirstChild("STL");
		if(m_prmRoot.Node()==NULL)
			return false;
		m_curNode=m_prmRoot.FirstChild("SCENE").Element();
		if(m_curNode==NULL)
			return false;

		return true;
	}

	bool Resource::LoadMenu(Menu * me)
	{		
		int32 id=-1;
		TiXmlElement* xe=m_prmRoot.FirstChild("SYSTEM_RES").FirstChild().Element();
		if(xe==NULL)
			return false;
		do
		{
			char fname[256]="./Res/Menu/System/";
			xe->Attribute("id",&id);
			strcat_s(fname,256,xe->Attribute("ext"));
			me->AddMenu(id,fname);
		}
		while(xe=xe->NextSiblingElement());
		id=-1;
		xe=m_prmRoot.FirstChild("FIGHT_RES").FirstChild().Element();
		if(xe==NULL)
			return false;
		do
		{
			char fname[256]="./Res/Menu/Fight/";
			xe->Attribute("id",&id);
			strcat_s(fname,256,xe->Attribute("ext"));
			me->AddMenu(id,fname);
		}
		while(xe=xe->NextSiblingElement());
		return true;
	}

	/*BOOL Resource::LoadTexture(TiXmlElement* xe)
	{
		TiXmlElement* group=xe->FirstChildElement();
		if(group==NULL)
			return FALSE;

		do
		{
			const char* kind = group->Value();
			TiXmlElement* se=group->FirstChildElement();
			if(se==NULL)
				return FALSE;
			do
			{
				int id=0;
				int state=0;

				TiXmlAttribute* at=se->FirstAttribute();
				if(at)
				{
					do
					{
						if(strcmp(at->Name(),"ID")==0)
						{
							id=atoi(at->Value());
						}
						else if(strcmp(at->Name(),"State")==0)
						{
							state=atoi(at->Value());
						}
					}while(at=at->Next());
				}
				const char* filename=se->GetText();
				if(!g_tex.AddTexture(filename,kind,id,state))
					g_err.SendErrorMessage(ERR_FUNC,"Texture::AddTexture");
			}
			while(se=se->NextSiblingElement());
		}
		while(group=group->NextSiblingElement());
		return TRUE;
	}*/

	/*BOOL Resource::LoadSound(TiXmlElement* xe)
	{
		return TRUE;
	}*/

	/*bool Resource::LoadScript(const char* file)
	{
		if (!m_scrdoc.LoadFile(file)) 
			return FALSE;
		m_pscrRoot=TiXmlHandle(&m_scrdoc);
		m_pscrRoot=m_pscrRoot.FirstChild("STL");
		if((m_pscrNode=m_pscrRoot.FirstChild().ToElement())==NULL)
		{
			//g_err.SendErrorMessage(ERR_FILE,"Script.xml");
		}
		return true;
	}*/

}